/*
 * MIT License
 *
 * Copyright (c) 2023-present, tangli
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package tony.knife4j.customizers

import io.swagger.v3.core.converter.AnnotatedType
import io.swagger.v3.core.converter.ModelConverters
import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.media.Content
import io.swagger.v3.oas.models.media.Schema
import java.lang.reflect.Type
import java.util.Optional
import org.slf4j.Logger
import org.springdoc.core.customizers.GlobalOperationComponentsCustomizer
import org.springframework.core.PriorityOrdered
import org.springframework.core.ResolvableType
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.method.HandlerMethod
import tony.core.SpringContexts
import tony.core.model.ApiResult
import tony.core.model.MonoValue
import tony.core.model.Rows
import tony.core.utils.antPathMatchAny
import tony.core.utils.asTo
import tony.core.utils.getLogger
import tony.core.utils.isArrayType
import tony.core.utils.isSimpleType
import tony.core.utils.isTypesOrSubTypesOf
import tony.core.utils.isVoidLikeType
import tony.core.utils.rawClass
import tony.knife4j.utils.SchemaParameterizedType
import tony.knife4j.utils.isDownloadType
import tony.knife4j.utils.typeParam

/**
 * 包装响应 Schema
 * @author tangli
 * @date 2025/07/23 11:40
 */
internal class WrapResponseBodyOperationCustomizer :
    GlobalOperationComponentsCustomizer,
    PriorityOrdered {
    private val logger: Logger = getLogger()

    override fun customize(
        operation: Operation,
        components: Components,
        handlerMethod: HandlerMethod,
    ): Operation {
        if (isInWrapResponseExcludePatterns(handlerMethod)) {
            return operation
        }

        val response = operation.responses["200"] ?: return operation
        val type = ResolvableType.forMethodReturnType(handlerMethod.method).type
        val returnType =
            if (type.isTypesOrSubTypesOf(
                    Optional::class.java,
                    ResponseEntity::class.java
                )
            ) {
                type.typeParam(0)
            } else {
                type
            }

        if (
            (response.content == null && !returnType.isVoidLikeType()) ||
            returnType.isTypesOrSubTypesOf(ApiResult::class.java) ||
            returnType.isSimpleType() ||
            isDownloadType(returnType)
        ) {
            return operation
        }

        if (returnType.isVoidLikeType()) {
            response.content =
                Content().addMediaType(
                    MediaType.APPLICATION_JSON_VALUE,
                    io.swagger.v3.oas.models.media
                        .MediaType()
                )
        }

        val mediaType = response.content[MediaType.APPLICATION_JSON_VALUE] ?: return operation
        val parameterizedType =
            when {
                returnType.isVoidLikeType() -> {
                    SchemaParameterizedType(
                        ApiResult::class.java,
                        Any::class.java
                    )
                }

                returnType.isTypesOrSubTypesOf(
                    MonoValue::class.java,
                    Collection::class.java
                ) -> {
                    SchemaParameterizedType(
                        ApiResult::class.java,
                        SchemaParameterizedType(returnType, returnType.typeParam(0))
                    )
                }

                returnType.isArrayType() -> {
                    SchemaParameterizedType(
                        ApiResult::class.java,
                        SchemaParameterizedType(
                            Rows::class.java,
                            returnType.rawClass().componentType
                        )
                    )
                }

                else -> {
                    SchemaParameterizedType(
                        ApiResult::class.java,
                        returnType
                    )
                }
            }
        if (components.schemas == null) {
            components.schemas = mutableMapOf()
        }
        val newSchema = generateSchema(parameterizedType, components) ?: return operation
        mediaType.schema =
            Schema<Any>().apply {
                `$ref` = "#/components/schemas/${newSchema.name}"
            }

        return operation
    }

    override fun customize(
        operation: Operation,
        handlerMethod: HandlerMethod,
    ): Operation {
        TODO()
    }

    private fun generateSchema(
        apiResultType: Type,
        components: Components,
    ): Schema<*>? {
        val resolvedSchema =
            ModelConverters
                .getInstance()
                .resolveAsResolvedSchema(AnnotatedType(apiResultType))
        if (resolvedSchema == null) {
            logger.warn("Unable to resolve schema for $apiResultType")
        }
        resolvedSchema?.referencedSchemas?.forEach { (name, schema) ->
            components.schemas.putIfAbsent(name, schema)
        }

        return resolvedSchema?.schema
    }

    private fun isInWrapResponseExcludePatterns(handlerMethod: HandlerMethod): Boolean {
        val requestMapping = handlerMethod.getMethodAnnotation(RequestMapping::class.java)
        val wrapResponseExcludePatterns =
            SpringContexts
                .Env
                .getProperty("dynamic.wrapResponseExcludePatterns", Set::class.java)
                .asTo<Set<String>>()
        val isInWrapResponseExcludePatterns =
            requestMapping?.path?.any { it.antPathMatchAny(wrapResponseExcludePatterns) } == true
        return isInWrapResponseExcludePatterns
    }

    override fun getOrder(): Int =
        PriorityOrdered.LOWEST_PRECEDENCE
}
